package org.example.main.utils;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema(description = "通用响应结果", name = "CommonResult")
public class CommonResult<T> {  // 添加泛型参数<T>
    @Schema(name = "code", description = "响应状态码")
    private int code; //响应状态码
    @Schema(name = "msg", description = "响应信息")
    private String msg; //响应信息
    @Schema(name = "data", description = "响应数据")
    private T data;  // 使用泛型类型T

    public CommonResult() {}

    public CommonResult(Integer code, String msg, T data) {  // 使用泛型类型T
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 成功响应的静态方法
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<>(200, "success", data);
    }

    // 失败响应的静态方法
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<>(500, message, null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {  // 返回泛型类型T
        return data;
    }

    public void setData(T data) {  // 使用泛型类型T
        this.data = data;
    }
}
